//
//  WetLandOrderDetailViewController.m
//  homeland
//
//  Created by SunShine on 14-1-25.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "WetLandOrderDetailViewController.h"
#import "WetlandService.h"
#import "BookingOrderDetailCell.h"

@interface WetLandOrderDetailViewController ()
@property (strong, nonatomic) NSArray *dataList;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation WetLandOrderDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"订单详情";
    NSString *presellid = [self.config objectForKey:@"presellid"];
    NSLog(@"传入的presellid为:%@", presellid);
    [self startLoadingViewWithText:@"正在读取订单详情"];
    
    
    //取订单id 如果为nil 请求后台
    if (self.orderId == nil) {
        //生成右侧按扭 getTicketsOrderId
//        self.navigationItem.rightBarButtonItem = getCustomNavButton(@"取没兴消订单", self, @selector(rightBtAction:));
        [[WetlandService new]getTicketsOrderId:[self.config objectForKey:@"createTime"] setPresellid:self.presellid Success:^(NSString *data) {
            self.orderId = data;
            
            if (self.orderId == nil) {
                [self stopLoadingView];
                return;
            }
            //生成右侧按扭,和请求服务器
            [self queryServer];
           
        } Failure:^(NSString *msg) {
            self.tableView.hidden=YES;
            [self showAlertWithTitle:@"请求失败" Msg:msg];
            [self stopLoadingView];
        }];
        
    }else{
        //生成右侧按扭,和请求服务器
        [self queryServer];
    }
   
}
//生成右侧按扭,和请求服务器 取得订单数据
-(void) queryServer{
    NSString *presellid = [self.config objectForKey:@"presellid"];
    //生成右侧按扭
    self.navigationItem.rightBarButtonItem = getCustomNavButton(@"取消订单", self, @selector(rightBtAction:));
    //取列表
    [[WetlandService new] getOrderDetailForId:presellid Success:^(NSArray *data) {
        self.dataList = data;
        [self.tableView reloadData];
        [self stopLoadingView];
    } Failure:^(NSString *msg) {
        [self showAlertWithTitle:@"错误" Msg:msg];
        [self stopLoadingView];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        //订单信息
        return 3;
    } else if (section == 1) {
        //票的名字
        return 1;
    } else if (section == 2) {
        //票面信息
        return (self.dataList?self.dataList.count:0);
    } else if (section == 3) {
        //时间地址
        return 3;
    } else {
        //联系方式
        return 2;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookingOrderDetailCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"BookingOrderDetailCell" owner:self options:nil] firstObject];
    if (indexPath.section == 0) {
        //订单信息
        if (indexPath.row == 0) {
            //订单状态
            cell.titleLabel.text = @"订单状态";
//            NSString *state = [self.config objectForKey:@"state"];
            NSString *state = [self getState:self.state];
            cell.infoLabel.text =state;
//            if (state && ![state isEqualToString:@"0"]) {
//                cell.infoLabel.text = @"未取票";
//            } else {
//                cell.infoLabel.text = @"已取票";
//            }
        } else if (indexPath.row == 1) {
            //订单编号
            cell.titleLabel.text = @"订单编号";
            cell.infoLabel.text =self.presellid;
//            cell.infoLabel.text = (self.dataList?[self.dataList[0] objectForKey:@"cpresellid"]:@"");
        } else {
            //预定日期
            cell.titleLabel.text = @"预定日期";
            NSString *createTime = [self.config objectForKey:@"createTime"];
            if (createTime) {
                cell.infoLabel.text = [createTime substringToIndex:10];
            } else {
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8*3600]];
                [formatter setDateFormat:@"yyyy-MM-dd"];
                cell.infoLabel.text = [formatter stringFromDate:[NSDate date]];
            }
        }
    } else if (indexPath.section == 1) {
        //票的名字
        cell.textLabel.text = @"广州海珠国家湿地公园";
        cell.titleLabel.text = @"";
        cell.infoLabel.text = @"";
    } else if (indexPath.section == 2) {
        //票面信息
        NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
        cell.titleLabel.text = [dic objectForKey:@"cticketname"];
        cell.infoLabel.text = [NSString stringWithFormat:@"%@ %@人", [dic objectForKey:@"ddatain"], [dic objectForKey:@"nhuman"]];
    } else if (indexPath.section == 3) {
        //时间地址
        if (indexPath.row == 0) {
            //营业时间
            cell.titleLabel.text = @"营业时间";
            cell.infoLabel.text = @"9:00-12:00";
        } else if (indexPath.row == 1) {
            //地址
            cell.titleLabel.text = @"地址";
            cell.infoLabel.text = @"海珠区新滘东";
        } else {
            //兑换
            cell.titleLabel.text = @"兑换方式";
            cell.infoLabel.text = @"凭门票预定信息在有效期内前往景区正门售票处验证换票，验证成功后即可进入景区游玩";
            cell.infoLabel.numberOfLines = 0;
        }
    } else {
        //联系方式
        if (indexPath.row == 0) {
            //取票人
            cell.titleLabel.text = @"取票人";
            cell.infoLabel.text = (self.dataList?[self.dataList[0] objectForKey:@"cname"]:@"");
        } else {
            //手机号码
            cell.titleLabel.text = @"手机号码";
            cell.infoLabel.text = (self.dataList?[self.dataList[0] objectForKey:@"ctel"]:@"");
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3 && indexPath.row == 2) {
        //兑换方式
        return 44.0*3;
    } else {
        return 44.0;
    }
}
-(NSString*)getState:(NSString*)state{
    if ([state isEqualToString:@"0"]) {
        state = @"未取票";
    }else if([state isEqualToString:@"1"]) {
        state = @"已取票";
    }else {
        state = @"已过期";
    }
    return state;
}
//取消订单事件
#pragma mark - Button Methods
-(void)rightBtAction:(id)sender
{
    //请求后台，如果删除成功，后退到上一页面
    [[WetlandService new] orderDelete:self.orderId Success:^(NSString *data) {
        BOOL flag = [data boolValue];
        if (flag) {
            NSLog(@"成功删除记录");
            //backMethod  后退
            [self backMethod];
            
        }
    }  Failure:^(BOOL msg) {
        
    }];
    
}
@end
